﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostSharp.Aspects;
using System.Diagnostics;
using Castle.DynamicProxy;

namespace Zadatak1.DynamicProxy
{
    public class FibonacciGenerator
    {
        public virtual int generate(int n)
        {
            return (n == 0 || n == 1) ? n : generate(n - 1) + generate(n - 2);
        }
    }
    
    class TimerAspect : IInterceptor
    {
        private Stopwatch stopwatch = new Stopwatch();

        public void Intercept(IInvocation invocation)
        {
            stopwatch.Start();
            invocation.Proceed();
            stopwatch.Stop();
            Console.WriteLine("DynamicProxy -- Method {0}: result={1}, duration={2}", invocation.Method.Name, invocation.ReturnValue, stopwatch.Elapsed);

        }
    }

    class ProgramDynamicProxy
    {
        public static void Run()
        {
            ProxyGenerator proxyGenerator = new ProxyGenerator();
            FibonacciGenerator generator = proxyGenerator.CreateClassProxyWithTarget<FibonacciGenerator>(new FibonacciGenerator(), new TimerAspect());
            Console.WriteLine("Fib(10)={0}", generator.generate(10));
        }
    }
}
